//
//  SNSummaryInfoCell.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/28.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNSummaryInfoViewController.h"
#import "SNLandInformationController.h"
#import "NSDate+TimeStamp.h"
#import "DVYearMonthDatePicker.h"
#import "SNSummaryInfoCell.h"
#import "SNPickerView.h"
#import "IQUIView+IQKeyboardToolbar.h"
//=****** add class ****************** stone ***
@interface SNSummaryInfoLabel : UILabel
/** 地块列表 */
@property (nonatomic, strong) NSArray<SNProductionPlanListLandList *> *landList;
@end

@implementation SNSummaryInfoLabel



@end
//=************************ stone ***
@interface SNSummaryInfoButton()

@end
@implementation SNSummaryInfoButton
- (void)addTextField
{
    UITextField * textField =  [UITextField new];
    self.textField = textField;
    
    [self addSubview:textField];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        [self addTextField];
    }
    return self;
}
-(void)setHighlighted:(BOOL)highlighted{
    
}

@end
//=************************ stone ***
@interface SNSummaryInfoCell()<DVYearMonthDatePickerDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet SNSummaryInfoLabel *rightLabel;

@property (weak, nonatomic) IBOutlet UILabel *importanceLabel;

@end
@implementation SNSummaryInfoCell
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.contentView.backgroundColor = HexRGB(0xffffff);
        
        [self addObserver];
    }
    return self;
}
- (void)receivedUITextFieldTextDidChangeNotification:(NSNotification *)notify
{
    NSLog(@"%@ | %s",self.class,__func__);
    [self checkTheShow];
}
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedUITextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.rightTextField];
 
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.iconButton addTarget:self action:@selector(iconButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)iconButtonClicked:(SNSummaryInfoButton *)sender
{
    if (sender.tag == 555) {
        SNLandInformationController * vc = [SNLandInformationController new];
        
        [self.nav pushViewController:vc animated:YES];
        
        return;
    }
    
    [sender.textField becomeFirstResponder];
}

- (void)configCellWithIndexPath:(NSIndexPath *)indexPath productionPlanListResult:(SNProductionPlanListResult *)productionPlanListResult
{
    switch (indexPath.section) {
        case 2:
            self.rightTextField.keyboardType = UIKeyboardTypeDecimalPad;
            break;
        default:
            self.rightTextField.keyboardType = UIKeyboardTypeDefault;
            break;
    }
    switch (indexPath.section) {
        case 0:
        {
            self.rightLabel.hidden = YES;
            self.titleLabel.text = @"作物种类";
            self.iconButton.hidden = NO;
            self.iconButton.textField.placeholder = @"作物种类";
            
            [self.iconButton.textField addLeftRightOnKeyboardWithTarget:self leftButtonTitle:@"取消" rightButtonTitle:@"确定" leftButtonAction:@selector(cropTypeNameCancel:) rightButtonAction:@selector(cropTypeNameConfirm:) shouldShowPlaceholder:YES];
            
            if (productionPlanListResult.cropTypeName.length > 0) {
                self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
                
                [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [self.iconButton setTitle:productionPlanListResult.cropTypeName forState:UIControlStateNormal];
                self.iconButton.textField.text = productionPlanListResult.cropTypeName;
                
                [self.iconButton setImage:nil forState:UIControlStateNormal];
                
                // self.previousTypeName = productionPlanListResult.cropTypeName;
            }else{
                self.iconButton.textField.text = @"玉米";
                [self.iconButton setImage:[UIImage imageNamed:@"06-next"] forState:UIControlStateNormal];
                [self.iconButton setTitleColor:nil forState:UIControlStateNormal];
                [self.iconButton setTitle:nil forState:UIControlStateNormal];
            }
            
            self.iconButton.textField.inputView = ^{
                NSArray<NSString *> * array =  @[@"玉米",@"大豆",@"小麦",@"水稻"];
                SNPickerView * pickerView = [[SNPickerView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 200) numberOfComponents:1 numberOfRows:4 models:array didSelectRowBlock:^(UIPickerView *pickerView, NSInteger row, NSInteger component) {
                    
                    self.iconButton.textField.text = array[row];
                    
                }];
                
                if (productionPlanListResult != nil && productionPlanListResult.cropTypeName.length > 0) {
                    NSInteger index = [array indexOfObject:productionPlanListResult.cropTypeName];
                    
                    [pickerView selectRow:index inComponent:0 animated:NO];
                    [pickerView reloadAllComponents];
                }
               
                return pickerView;
            }();
            
            self.rightTextField.hidden = YES;
            self.importanceLabel.hidden = NO;
        }
            break;
        case 1:
        {
            self.rightLabel.hidden = YES;
            self.titleLabel.text = @"种植时间";
            
            self.iconButton.hidden = NO;
            self.iconButton.textField.placeholder = @"种植时间";
            self.iconButton.textField.inputView = ^{
                
                DVYearMonthDatePicker *yearMonth = [[DVYearMonthDatePicker alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
                yearMonth.backgroundColor = [UIColor whiteColor];
                yearMonth.dvDelegate = self;
                if (productionPlanListResult != nil && productionPlanListResult.startTime.length > 0) {
                    
                    [yearMonth selectDate:productionPlanListResult.startTime.toDate(@"yyyy-MM")];
                }else{
                    [yearMonth selectToday];
                }
                
                return yearMonth;
            }();
            
            [self.iconButton.textField addLeftRightOnKeyboardWithTarget:self leftButtonTitle:@"取消" rightButtonTitle:@"确定" leftButtonAction:@selector(startTimeCancel:) rightButtonAction:@selector(startTimeConfirm:) shouldShowPlaceholder:YES];
            
            if (productionPlanListResult.startTime.length > 0) {
                self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
                
                [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [self.iconButton setTitle:productionPlanListResult.startTime forState:UIControlStateNormal];
                
                self.iconButton.textField.text = productionPlanListResult.startTime;
                
                [self.iconButton setImage:nil forState:UIControlStateNormal];
                
                // self.previousTypeName = productionPlanListResult.startTime;
            }else{
                
                self.iconButton.textField.text = [NSDate new].dateToString(@"yyyy-MM");
                
                [self.iconButton setImage:[UIImage imageNamed:@"06-calendar"] forState:UIControlStateNormal];
                [self.iconButton setTitleColor:nil forState:UIControlStateNormal];
                [self.iconButton setTitle:nil forState:UIControlStateNormal];
            }
            
            
            self.rightTextField.hidden = YES;
            self.importanceLabel.hidden = NO;
        }
            break;
        case 2:
            self.rightLabel.hidden = YES;
            self.titleLabel.text = @"种植面积";
            
            self.iconButton.hidden = YES;
            self.rightTextField.hidden = NO;
            self.rightTextField.placeholder = @"公顷";
            self.rightTextField.text = productionPlanListResult.plantingArea;
            self.importanceLabel.hidden = NO;
            break;
        case 3:
            
            if (productionPlanListResult.landList.count > 0) {
                self.rightLabel.hidden = NO;
                self.rightLabel.text = productionPlanListResult.landList.firstObject.landLocation;
            }else{
                self.rightLabel.hidden = YES;
                {
                    UIView * mmm = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6.5, 12)];
                    
                    UIImageView * imageView = [[UIImageView alloc] init];
                    UIImage * image = [UIImage imageNamed:@"06-next"];
                    imageView.image = image;
                    imageView.frame = CGRectMake(-10, 0, 6.5, 12);
                    
                    [mmm addSubview:imageView];
                    
                    self.accessoryView = mmm;
                }
            }
            
            
            self.titleLabel.text = @"地块信息";
            self.iconButton.hidden = YES;
          
            
        
//            [self.iconButton setImage:[UIImage imageNamed:@"06-next"] forState:UIControlStateNormal];
//            self.iconButton.tag = 555;
            /** 地块信息 */
            // productionPlanListResult.landList
            
            self.rightTextField.hidden = YES;
            self.importanceLabel.hidden = YES;
            break;
        default:
            break;
    }
}
- (void)checkTheShow
{
    NSInteger count = 0;
    UITableView * tableView = (UITableView *)self.superview.superview;
    for(int i = 0; i < 3; i++) {
        SNSummaryInfoCell * cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
        
        if (i==2) {
            if (cell.rightTextField.text.length > 0) {
                count ++;
            }
        }else{
            if (cell.iconButton.currentTitle.length > 0) {
                count++;
            }
        }
    }
    
    if (count == 3) {
        [SNSummaryInfoViewController getSaveButton].enabled = YES;
    }else{
        [SNSummaryInfoViewController getSaveButton].enabled = NO;
    }
}
- (void)startTimeConfirm:(id)sender
{
    [self.iconButton.textField resignFirstResponder];
    
    self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.iconButton setTitle:self.iconButton.textField.text forState:UIControlStateNormal];
    [self.iconButton setImage:nil forState:UIControlStateNormal];
    
    [self checkTheShow];
}
- (void)startTimeCancel:(id)sender
{
    [self.iconButton.textField resignFirstResponder];
    
}
//sn_note:=========  ============================ stone 🐳 ===========/
- (void)cropTypeNameConfirm:(id)sender
{
    [self.iconButton.textField resignFirstResponder];
    self.iconButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.iconButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.iconButton setTitle:self.iconButton.textField.text forState:UIControlStateNormal];
    [self.iconButton setImage:nil forState:UIControlStateNormal];
    
    [self checkTheShow];
}
- (void)cropTypeNameCancel:(id)sender
{
    [self.iconButton.textField resignFirstResponder];
}




- (void)yearMonthDatePicker:(DVYearMonthDatePicker *)yearMonthDatePicker didSelectedDate:(NSDate *)date {
    
    NSLog(@"%@", date);
    self.iconButton.textField.text = date.dateToString(@"yyyy-MM");
    
}
@end
